package cn.felord.debezium.controller;

import cn.felord.debezium.dao.EquipmentDetectionStandardMapper;
import cn.felord.debezium.dao.EquipmentInfoMapper;
import cn.felord.debezium.dao.HSfAncillaryMateriasMapper;
import cn.felord.debezium.dao.HSfMaterialsMapper;
import cn.felord.debezium.entity.EquipmentDetectionStandard;
import cn.felord.debezium.entity.EquipmentInfo;
import cn.felord.debezium.entity.HSfAncillaryMaterias;
import cn.felord.debezium.entity.HSfMaterials;
import cn.felord.debezium.service.ScaleEquipmentMamageService;
import cn.felord.debezium.task.R;
import cn.felord.debezium.utils.AuToSend;
import cn.felord.debezium.utils.CRC16Checksum;
import cn.felord.debezium.utils.String2AcsII16;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import static cn.felord.debezium.common.Watch.*;
@Slf4j
@RestController
@RequestMapping("/down")
public class DownTcpHjcController {
    @Autowired
    EquipmentInfoMapper equipmentInfoMapper;
    @Autowired
    EquipmentDetectionStandardMapper equipmentDetectionStandardMapper;
    @Autowired
    HSfMaterialsMapper hSfMaterialsMapper;
    @Autowired
    HSfAncillaryMateriasMapper hSfAncillaryMateriasMapper;
    @Autowired
    ScaleEquipmentMamageService scaleEquipmentMamageService;
    @Autowired
    AuToSend auToSend;
    @RequestMapping("/downEquipInfo")
    public R downEquipInfo(String equipCode) throws IOException {
        if(equipCode == null){
            return R.failure("参数equipCode必传");
        }
        EquipmentInfo equipmentInfo = equipmentInfoMapper.selectEquipmentInfoByEquipmentCode(equipCode);
        if(null == equipmentInfo){
            return R.failure("设备信息不存在");
        }
        String upModel = equipmentInfo.getUpModel();
        if(upModel.equals("4G")){
            upModel = UP_MODEL_4G;
        }else if(upModel.equals("LORA")){
            upModel = UP_MODEL_LORA;
        }else if(upModel.equals("wifi")){
            upModel = UP_MODEL_WIFI;
        }
        String conIp = equipmentInfo.getConIp();
        String ip1 = "1";
        String ip2 = "1";
        String ip3 = "1";
        String ip4 = "1";
        if(conIp != null && conIp.split("\\.").length == 4){
            ip1 = conIp.split("\\.")[0];
            ip2 = conIp.split("\\.")[1];
            ip3 = conIp.split("\\.")[2];
            ip4 = conIp.split("\\.")[3];
        }
        String conAcc = equipmentInfo.getConAcc();
        if(conAcc.equals("C3")){
            conAcc = SCAL_C3;
        }else if(conAcc.equals("C6")){
            conAcc = SCAL_C6;
        }
        String begin = "0110"+"0300"+"002C"+"0058"
                + String2AcsII16.string2hex(equipmentInfo.getEquipType(), 8)
                + equipmentInfo.getEquipNum() + "00000001" + "00000001"
                + upModel
                + String2AcsII16.int10to16_4(Integer.valueOf(ip1))
                + String2AcsII16.int10to16_4(Integer.valueOf(ip2))
                + String2AcsII16.int10to16_4(Integer.valueOf(ip3))
                + String2AcsII16.int10to16_4(Integer.valueOf(ip4))
                + String2AcsII16.int10to16(Integer.valueOf(equipmentInfo.getConPort()),8)
                + String2AcsII16.int10to16(equipmentInfo.getConHeart().intValue(),4)
                + conAcc
                + String2AcsII16.int10to16(equipmentInfo.getConMileage().intValue(),8)
                + "00000001"+"00000000"//IMIE 不覆盖，不传，只占位
                + "00000001"+"00000000"
                + equipmentInfo.getCardNum()
                +"00000001"
                +"00000001"
                +"00000001";
        String crc = CRC16Checksum.getCrc16ModBus(begin);
        String send  = "FE" + begin + crc + "FF";
        return auToSend.sendEquip(send,equipCode);
    }
    /**
     * 下发产前【辅料新字符/主料/质检标准】
     * @param equipCode
     */
    @RequestMapping("/downMaterial")
    public R downMaterial(String equipCode) throws IOException {
        if(equipCode == null){
            return R.failure("参数equipCode必传");
        }

        List<HSfMaterials> hSfMaterials = hSfMaterialsMapper.selectHSfMaterialsByEquipCode(equipCode);
        if(hSfMaterials == null || hSfMaterials.size() ==0){
            return R.failure("设备编码不存在对应元素信息，请在元素标准中补全信息！");
        }
        HashMap<String, HSfMaterials> hSfMaterialsHashMap = new HashMap<>();
        List<HSfAncillaryMaterias> hSfAncillaryMaterias = hSfAncillaryMateriasMapper.selectHSfAncillaryMateriasByEquipCode(equipCode);
        HashMap<String, HSfAncillaryMaterias> hSfAncillaryMateriasHashMap = new HashMap<>();
        for (HSfMaterials hSfMaterials1 : hSfMaterials) {
            hSfMaterialsHashMap.put(hSfMaterials1.getmName(),hSfMaterials1);
        }
        for (HSfAncillaryMaterias hSfAncillaryMateria : hSfAncillaryMaterias) {
            hSfAncillaryMateriasHashMap.put(hSfAncillaryMateria.geteName(),hSfAncillaryMateria);
        }
        EquipmentDetectionStandard equipmentDetectionStandard = equipmentDetectionStandardMapper
                .selectEquipmentDetectionStandardByEquipCode(equipCode);
        if(equipmentDetectionStandard == null){
            log.error("下发失败，元素标准[C/Si/]缺失！");
            return R.failure("下发失败，元素标准[C/Si/]缺失！");
        }
        String equipType = equipmentDetectionStandard.getEquipType();
        String equipNum = equipmentDetectionStandard.getEquipNum();
        if(hSfMaterialsHashMap.get("HL") == null
                ||hSfMaterialsHashMap.get("FG")== null
                ||hSfMaterialsHashMap.get("ST")== null
                ||hSfMaterialsHashMap.get("GFG")== null//高个废钢
                ){
            log.error("主料FG/HL/ST/GFG主料缺失，元素缺失异常");
            return R.failure("主料FG/HL/ST/GFG主料缺失，元素缺失异常");
        }
        if(hSfAncillaryMateriasHashMap.get("SLC") == null//增碳剂
                ||hSfAncillaryMateriasHashMap.get("SLSi")== null//硅粒
                ||hSfAncillaryMateriasHashMap.get("SiCC")== null//碳化硅碳
                ||hSfAncillaryMateriasHashMap.get("SiCS")== null//碳化硅硅
                ){
            log.error("新辅料【SLC/SLSi/SiCC/SiCS】元素缺失异常");
            return R.failure("新辅料【SLC/SLSi/SiCC/SiCS】元素缺失异常");
        }

        BigDecimal b100 = new BigDecimal(100);
        String begin = "0110" + "0500" +"002A" +"0084"
                + String2AcsII16.string2hex(equipType,8)
                + equipNum
                + "00000000"
                + String2AcsII16.string2hex("HLC",8)
                + "0000"
                + String2AcsII16.int10to16(hSfMaterialsHashMap.get("HL").getMC().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.string2hex("HLSi",8)
                + "0000"
                + String2AcsII16.int10to16(hSfMaterialsHashMap.get("HL").getmSi().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.string2hex("FGC",8)
                + "0000"
                + String2AcsII16.int10to16(hSfMaterialsHashMap.get("FG").getMC().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.string2hex("FGSi",8)
                + "0000"
                + String2AcsII16.int10to16(hSfMaterialsHashMap.get("FG").getmSi().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.string2hex("STC",8)
                + "0000"
                + String2AcsII16.int10to16(hSfMaterialsHashMap.get("ST").getMC().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.string2hex("STSi",8)
                + "0000"
                + String2AcsII16.int10to16(hSfMaterialsHashMap.get("ST").getmSi().multiply(b100).setScale(0).intValue(),4)

                + String2AcsII16.string2hex("BZC",8)
                +"0000"
                + String2AcsII16.int10to16(new BigDecimal(equipmentDetectionStandard.getC()).multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.string2hex("BZSi",8)
                +"0000"
                + String2AcsII16.int10to16(new BigDecimal(equipmentDetectionStandard.getSi()).multiply(b100).setScale(0).intValue(),4)

                + String2AcsII16.string2hex("SiCC",8)
                + String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("SiCC").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("SiCC").geteEffective().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.string2hex("SiCS",8)
                + String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("SiCS").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("SiCS").geteEffective().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.string2hex("SLC",8)
                + String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("SLC").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("SLC").geteEffective().multiply(b100).intValue(),4)
                + String2AcsII16.string2hex("SLSi",8)
                + String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("SLSi").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("SLSi").geteEffective().multiply(b100).setScale(0).intValue(),4)

                + String2AcsII16.string2hex("GFGC",8)
                + "0000"
                + String2AcsII16.int10to16(hSfMaterialsHashMap.get("GFG").getMC().multiply(b100).setScale(0).intValue(),4)
                + String2AcsII16.string2hex("GFGS",8)
                + "0000"
                + String2AcsII16.int10to16(hSfMaterialsHashMap.get("GFG").getmSi().multiply(b100).setScale(0).intValue(),4)

                +"00000000"
                +"00000000";
        String crc = CRC16Checksum.getCrc16ModBus(begin);
        String send  = "FE" + begin.toUpperCase() + crc + "FF";
        return auToSend.sendEquip(send,equipCode);

    }
    /**
     * 下发元素
     * @param equipCode
     */
    @RequestMapping("/downAncillaryMaterial")
    public R downAncillaryMaterial(String equipCode) throws IOException {
        if(equipCode == null){
            return R.failure("参数equipCode必传");
        }
        List<HSfAncillaryMaterias> hSfAncillaryMaterias = hSfAncillaryMateriasMapper.selectHSfAncillaryMateriasByEquipCode(equipCode);
        HashMap<String, HSfAncillaryMaterias> hSfAncillaryMateriasHashMap = new HashMap<>();
        String equipType = "KDHC";
        String equipNum = "00000000";
        for (HSfAncillaryMaterias hSfAncillaryMateria : hSfAncillaryMaterias) {
            hSfAncillaryMateriasHashMap.put(hSfAncillaryMateria.geteName(),hSfAncillaryMateria);
            equipType = hSfAncillaryMateria.getEquipType();
            equipNum = hSfAncillaryMateria.getEquipNum();
        }
        if(hSfAncillaryMateriasHashMap.get("C") == null
            ||hSfAncillaryMateriasHashMap.get("Si")== null
            ||hSfAncillaryMateriasHashMap.get("Mn")== null
            ||hSfAncillaryMateriasHashMap.get("Sn")== null
            ||hSfAncillaryMateriasHashMap.get("Cu")== null
            ||hSfAncillaryMateriasHashMap.get("S")== null
            ||hSfAncillaryMateriasHashMap.get("Cr")== null
            ||hSfAncillaryMateriasHashMap.get("Al")== null
            ||hSfAncillaryMateriasHashMap.get("Ni")== null){
            log.error("C-Mn-S-Sn Cu_s_Cr_Al_Ni元素缺失异常");
            return R.failure("C-Mn-S-Sn Cu_s_Cr_Al_Ni元素缺失异常") ;
        }
        BigDecimal b100 = new BigDecimal(100);
        String begin = "0110"+"0400"+"002C"+"0058"
                + String2AcsII16.string2hex_UP(equipType,8)
                + equipNum
                +"00000001"
                +String2AcsII16.string2hex("C",8)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("C").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("C").geteEffective().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.string2hex("Si",8)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Si").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Si").geteEffective().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.string2hex("Mn",8)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Mn").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Mn").geteEffective().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.string2hex("Sn",8)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Sn").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Sn").geteEffective().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.string2hex("Cu",8)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Cu").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Cu").geteEffective().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.string2hex("S",8)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("S").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("S").geteEffective().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.string2hex("Cr",8)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Cr").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Cr").geteEffective().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.string2hex("Al",8)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Al").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Al").geteEffective().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.string2hex("Ni",8)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Ni").geteAbsorb().multiply(b100).setScale(0).intValue(),4)
                +String2AcsII16.int10to16(hSfAncillaryMateriasHashMap.get("Ni").geteEffective().multiply(b100).setScale(0).intValue(),4)
                + "00000001";
        String crc = CRC16Checksum.getCrc16ModBus(begin);
        String send  = "FE" + begin.toUpperCase() + crc + "FF";
        return auToSend.sendEquip(send,equipCode);
    }
    /**
     * 下发产后
     * @param equipCode
     */
    @RequestMapping("/downAfter")
    public R downAfter(String equipCode) throws IOException {
        if(equipCode == null){
            return R.failure("参数equipCode必传");
        }
        EquipmentDetectionStandard equipmentDetectionStandard = equipmentDetectionStandardMapper.selectEquipmentDetectionStandardByEquipCode(equipCode);
        if(equipmentDetectionStandard == null){
            log.error("元素标准不存在");
            return R.failure("元素标准不存在");
        }
        BigDecimal b100 = new BigDecimal(100);
        String begin = "0110"+"0600"+"000E" + "001C"
                + String2AcsII16.string2hex(equipmentDetectionStandard.getEquipType(),8)
                + equipmentDetectionStandard.getEquipNum()
                +"00000000"
                + String2AcsII16.string2hex("QHSi",8)
                + "0000"
                + String2AcsII16.int10to16(new BigDecimal(equipmentDetectionStandard.getLable1()).multiply(b100).setScale(0).intValue(),4)
                +"00000000"
                +"00000000";
        String crc = CRC16Checksum.getCrc16ModBus(begin);
        String send  = "FE" + begin.toUpperCase() + crc + "FF";
        return auToSend.sendEquip(send,equipCode);
    }

    @RequestMapping("/downLh")
    public R downLh(String equipCode) throws IOException {
        return scaleEquipmentMamageService.push_update(equipCode);
    }
}
